using System;
using System.Collections.Generic;
using System.Text;

namespace GoogleCalendarReminder
{
    /// <summary>
    /// A class which contains a collection of GoogleCalendarReminder.EventEntryWrapper objects.
    /// </summary>
    public class EventEntryWrapperCollection: IEnumerable<EventEntryWrapper>
    {
        #region Private Data Members

        private List<EventEntryWrapper> _list = new List<EventEntryWrapper>();

        #endregion

        #region Private Functions

        /// <summary>
        /// Determines whether or not an item exists in the collection.
        /// </summary>
        /// <param name="item">The item to find.</param>
        /// <returns>true if the item exists; false otherwise.</returns>
        private bool contains(EventEntryWrapper item)
        {
            foreach (EventEntryWrapper ev in _list)
            {
                if (ev.EventId.Equals(item.EventId))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Gets the index of a given item.
        /// </summary>
        /// <param name="item">The item to find.</param>
        /// <returns>The index of the item; -1 if the item wasn't found.</returns>
        private int indexOf(EventEntryWrapper item)
        {
            for (int i = 0; i < _list.Count; i++)
            {
                if (_list[i].EventId.Equals(item.EventId))
                {
                    return i;
                }
            }

            return -1;
        }

        #endregion

        #region Protected Functions

        /// <summary>
        /// Handles the ListChanged event.
        /// </summary>
        protected virtual void OnListChanged(EventArgs e)
        {
            if (ListChanged != null)
            {
                ListChanged(this, e);
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new instance of the GoogleCalendarReminder.EventEntryWrapperCollection class.
        /// </summary>
        public EventEntryWrapperCollection() { }

        #endregion

        #region Public Functions

        /// <summary>
        /// Adds an item to the collection.
        /// </summary>
        /// <param name="item">The item to add.</param>
        public void Add(EventEntryWrapper item)
        {
            int index = indexOf(item);

            if (index == -1)
            {
                _list.Add(item);
            }
            else
            {
                _list[index] = item;
            }

            OnListChanged(EventArgs.Empty);
        }

        /// <summary>
        /// Removes all items from the collection.
        /// </summary>
        public void Clear()
        {
            _list.Clear();
            OnListChanged(EventArgs.Empty);
        }

        /// <summary>
        /// Inserts an item into the collection.
        /// </summary>
        /// <param name="index">The index to place the item.</param>
        /// <param name="item">The item to insert.</param>
        public void Insert(int index, EventEntryWrapper item)
        {
            _list.Insert(index, item);
            OnListChanged(EventArgs.Empty);
        }

        /// <summary>
        /// Removes an item from the collection.
        /// </summary>
        /// <param name="item">The item to remove.</param>
        /// <returns>true if the item was removed; false if it wasn't (didn't exist in the collection).</returns>
        public bool Remove(EventEntryWrapper item)
        {
            bool isRemoved = _list.Remove(item);
            if (isRemoved) OnListChanged(EventArgs.Empty);
            return isRemoved;
        }

        /// <summary>
        /// Sorts the items in the list.
        /// </summary>
        /// <param name="comparison">The delegate that performs the comparisons.</param>
        public void Sort(Comparison<EventEntryWrapper> comparison)
        {
            _list.Sort(comparison);
        }

        /// <summary>
        /// Converts the collection into an array.
        /// </summary>
        /// <returns>An array of object in the collection.</returns>
        public EventEntryWrapper[] ToArray()
        {
            return _list.ToArray();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets an item in the collection.
        /// </summary>
        /// <param name="index">The index to get or set.</param>
        /// <returns>The item at that index.</returns>
        public EventEntryWrapper this[int index]
        {
            get { return _list[index]; }
            set
            {
                _list[index] = value;
                OnListChanged(EventArgs.Empty);
            }
        }

        /// <summary>
        /// Gets the number of items in the collection.
        /// </summary>
        public int Count
        {
            get { return _list.Count; }
        }

        #endregion

        #region Public Events

        /// <summary>
        /// Occurs when items in the the collection change.
        /// </summary>
        public event EventHandler ListChanged;

        #endregion

        #region IEnumerable<EventEntryWrapper> Members

        public IEnumerator<EventEntryWrapper> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion
    }
}
